﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_indexSet : OTS.AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TestPurview(HttpContext.Current, OTS.enumSysRight.IndexSet);
        if (!Page.IsPostBack)
        {
            DoPageBind();
        }
    }
    private void DoPageBind()
    {
        OTS.BLL.IndexPicBLL bll = new OTS.BLL.IndexPicBLL();
        DataTable dt = bll.GetList("");
        foreach(DataRow dr in dt.Rows)
        {
            dr["picPath"] = PictureProcessing.GetThumbURL(dr["picPath"].ToString());
        }
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.txtURL.Text == "")
        {
            OTS.Common.Jscript.Alert("链接不能为空");
            return;
        }
        HttpPostedFile oFile = this.upPic.PostedFile;
        string strFilePath = PictureProcessing.SaveIndexPics(oFile, OTS.Common.Help.GetUniqueKey());
        if (strFilePath == "")
        {
            OTS.Common.Jscript.Alert("上传的图片有问题！请重新上传！");
            return;
        }
        PictureProcessing.SaveIndexFileThumPic(strFilePath, PictureProcessing.GetThumbPath(strFilePath));
        OTS.Model.indexPic model = new OTS.Model.indexPic();
        model.picPath  = strFilePath;
        model.picURL = this.txtURL.Text;
        OTS.Model.SessionUser oUser =    getSessionUser(HttpContext.Current);
        model.picUserID = oUser.uid;

        OTS.BLL.IndexPicBLL bll = new OTS.BLL.IndexPicBLL();
        if(bll.Add(model))
            DoPageBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string strID = Request.Form["txtID"];
        if (strID == null)
        {
            OTS.Common.Jscript.Alert("非法操作！");
            return;
        }
        if (!OTS.Common.Help.IsNumber(strID))
        {
            OTS.Common.Jscript.Alert("非法操作！");
            return;
        }
        OTS.BLL.IndexPicBLL bll = new OTS.BLL.IndexPicBLL();
        OTS.Model.indexPic model = bll.GetModel(int.Parse(strID));
        if (model == null)
        {
            OTS.Common.Jscript.Alert("记录不存在！");
            return;
        }

        string strSrcPath = System.Web.HttpContext.Current.Server.MapPath(model.picPath);
        try
        {
            System.IO.File.Delete(strSrcPath);
        }
        catch { }
        try
        { System.IO.File.Delete(PictureProcessing.GetThumbPath(model.picPath)); }
        catch { }
        bll.Delete(int.Parse(strID));
        DoPageBind();
        
    }
}
